package cn.xyfmzmy.decorator.set;

import java.util.*;

/**
 * 作用：记录被remove的元素
 * 装饰器的本质：对对象进行增强，而不改变它原本的功能
 * @param <E>
 */
public class HistorySet<E> implements Set<E> {

    // 记录被remove的元素
    List<E> removeList = new ArrayList<>();

    // 真正存放元素的set集合，组合优于继承
    private Set<E> delegate;

    public HistorySet(Set<E> set) {
        this.delegate = set;
    }

    @Override
    public int size() {
        return delegate.size();
    }

    @Override
    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return delegate.contains(o);
    }

    @Override
    public Iterator<E> iterator() {
        return delegate.iterator();
    }

    @Override
    public Object[] toArray() {
        return delegate.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return delegate.toArray(a);
    }

    @Override
    public boolean add(E e) {
        return delegate.add(e);
    }

    @Override
    public boolean remove(Object o) {
        if(delegate.remove(o)){
            removeList.add((E) o);
            return true;
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return delegate.containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return delegate.addAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return delegate.retainAll(c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return delegate.removeAll(c);
    }

    @Override
    public void clear() {

    }

    @Override
    public String toString() {
        return " curList: " + delegate.toString() + "\n removeList: " + removeList.toString();
    }
}
